package org.abratuhi.kvngroupware.client.model;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.abratuhi.kvngroupware.brainstorm.manager.BrainstormManager;
import org.abratuhi.kvngroupware.brainstorm.mptua.BrainstormResponseMessageProceeder;
import org.abratuhi.kvngroupware.chat.manager.ChatroomManager;
import org.abratuhi.kvngroupware.chat.mptua.ChatroomResponseMessageProceeder;
import org.abratuhi.kvngroupware.dalgen.DalgenManager;
import org.abratuhi.kvngroupware.dalgen.DalgenResponseMessageProceeder;
import org.abratuhi.kvngroupware.jokemanager.manager.JokeManager;
import org.abratuhi.kvngroupware.jokemanager.mptua.JokemanagerResponseMessageProceeder;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.login.mptua.LoginResponseMessageProceeder;
import org.abratuhi.kvngroupware.network.C_Client;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageProceeder;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.registration.manager.RegistrationManager;
import org.abratuhi.kvngroupware.registration.mptua.RegistrationResponseMessageProceeder;
import org.abratuhi.kvngroupware.scenario.manager.ScenarioManager;
import org.abratuhi.kvngroupware.scenario.mptua.ScenarioJokeResponseMessageProceeder;
import org.abratuhi.kvngroupware.scenario.mptua.ScenarioResponseMessageProceeder;
import org.abratuhi.kvngroupware.team.manager.TeamManager;
import org.abratuhi.kvngroupware.team.model.Team;
import org.abratuhi.kvngroupware.team.mptua.TeamResponseMessageProceeder;
import org.abratuhi.kvngroupware.user.manager.UserManager;
import org.abratuhi.kvngroupware.user.model.User;
import org.abratuhi.kvngroupware.user.mptua.UserResponseMessageProceeder;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

public class KVNGroupwareClient implements MessageProceeder, MessageSender{
	
	public final static String localeFile = "locale.xml";
	public final static String clientFile = "client.properties";
	
	public Properties locale;
	public Properties clientprops;
	
	User user = new User();
	Team team = new Team();
	
	C_Client netclient;
	
	public LoginManager loginmanager = new LoginManager();
	public RegistrationManager regmanager = new RegistrationManager();
	public UserManager usermanager = new UserManager();
	public TeamManager teammanager = new TeamManager();
	public JokeManager jokemanager = new JokeManager();
	public ScenarioManager scenariomanager = new ScenarioManager();
	public ChatroomManager chatroommanager = new ChatroomManager();
	public BrainstormManager brainstormmanager = new BrainstormManager();
	public DalgenManager dalgenmanager = new DalgenManager();
	public WidgetManager widgetmanager = new WidgetManager();
	
	LoginResponseMessageProceeder loginmresponder;
	RegistrationResponseMessageProceeder registrationmresponder;
	UserResponseMessageProceeder usermresponder;
	TeamResponseMessageProceeder teammresponder;
	ChatroomResponseMessageProceeder chatroommresponder;
	BrainstormResponseMessageProceeder brainstormmresponder;
	ScenarioResponseMessageProceeder scenariomresponder;
	ScenarioJokeResponseMessageProceeder scenariojokemresponder;
	JokemanagerResponseMessageProceeder jokemanagermresponder;
	DalgenResponseMessageProceeder dalgenmresponder;
	
	
	public KVNGroupwareClient(){
		locale = loadProperties(localeFile);
		clientprops = loadProperties(clientFile);
		netclient = new C_Client(clientprops.getProperty("client.server.host"), Integer.valueOf(clientprops.getProperty("client.server.port")), this);
		
		loginmresponder = new LoginResponseMessageProceeder(this, loginmanager, usermanager, widgetmanager);
		registrationmresponder = new RegistrationResponseMessageProceeder(regmanager, widgetmanager);
		usermresponder = new UserResponseMessageProceeder(this, this.locale, loginmanager, usermanager);
		teammresponder = new TeamResponseMessageProceeder(this, this.locale, loginmanager, teammanager);
		chatroommresponder = new ChatroomResponseMessageProceeder(this, this.locale, loginmanager, chatroommanager, widgetmanager);
		brainstormmresponder = new BrainstormResponseMessageProceeder(this, this.locale, loginmanager, brainstormmanager, widgetmanager);
		scenariomresponder = new ScenarioResponseMessageProceeder(this, this.locale, loginmanager, scenariomanager, jokemanager, widgetmanager);
		scenariojokemresponder = new ScenarioJokeResponseMessageProceeder(this, this.locale, loginmanager, scenariomanager, widgetmanager);
		jokemanagermresponder = new JokemanagerResponseMessageProceeder(this, this.locale, loginmanager, jokemanager, widgetmanager);
		dalgenmresponder = new DalgenResponseMessageProceeder(dalgenmanager, widgetmanager);
	}
	
	/**/
	
	public void proceedMessage(Client client, Message message) {
		//System.out.println("KVNGROUPWARE_CLIENT[recv]: "+message.toString());
		loginmresponder.proceedMessage(client, message);
		registrationmresponder.proceedMessage(client, message);
		usermresponder.proceedMessage(client, message);
		teammresponder.proceedMessage(client, message);
		chatroommresponder.proceedMessage(client, message);
		brainstormmresponder.proceedMessage(client, message);
		scenariomresponder.proceedMessage(client, message);
		scenariojokemresponder.proceedMessage(client, message);
		jokemanagermresponder.proceedMessage(client, message);
		dalgenmresponder.proceedMessage(client, message);
	}
	
	public Message createMessage(String[] attrs, String[] values, String text, Element[] contents){
		Message message = new Message();
		//Element from = new Element("from");
		//from.setAttribute("type", "uid");
		//from.setAttribute("id", loginmanager.getUser().getId());
		//Element to = new Element("to");
		//to.setAttribute("type", "uid");
		//to.setAttribute("id", "system");
		Element request = new Element("request");
		request.setText(text);
		for(int i=0; i<contents.length; i++)
			request.addContent(contents[i]);
		for(int i=0; i<attrs.length; i++)
			request.setAttribute(attrs[i], values[i]);
		//message.getRoot().addContent(from);
		//message.getRoot().addContent(to);
		message.getRoot().addContent(request);
		return message;
	}
	
	public Message createMessage(String attrs, String values, String text, Element[] contents){
		return createMessage(attrs.split(","), values.split(","), text, contents);
	}
	
	public void sendMessage(Message message){
		//System.out.println("KVNGROUPWARE_CLIENT[send]: "+message.toString());
		netclient.sendMessage(message);
		//netclient.sendString(message.toString());
	}
	
	public void sendMessage(Message message, String[] recipientIds){
		sendMessage(message);
	}
	
	// *********
	// something
	// *********
	public void start(){
		this.netclient.start();
	}
	
	public void stop(){
		this.netclient.stop();
	}
	
	public Properties loadProperties(String file){
		Properties props = new Properties();
		try {
			if(file.endsWith(".xml")){
		        props.loadFromXML(new FileInputStream(file));
			}
			else if(file.endsWith(".properties") || file.endsWith(".preferences")){
				props.load(new FileInputStream(file));
			}
	    } catch (IOException e) {
	    	e.printStackTrace();
	    }
	    return props;
	}
	
	// ****
	// main
	// ****
	/*public static void main(String[] args){
		final KVNGroupwareClient kvnclient = new KVNGroupwareClient();
		kvnclient.start();
		kvnclient.widgetmanager.put("root", new GuiRoot("", kvnclient, kvnclient.locale, kvnclient.loginmanager, kvnclient.regmanager));
		SwingUtilities.invokeLater(new Runnable(){
			@Override
			public void run() {
				kvnclient.widgetmanager.get("root").createWindow();
			}
		});
	}*/

}
